import cgi
import os
import sys

from mod_python import apache, util

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..','modules')))
import mods


def index(req):
	web = mods.Web(req)
	cursor = mods.GetDBConnection().cursor()

	message = ''

	logout = web.GetNumber('logout')
	if logout:
		web.SetCookie('key', '')
		web.Redirect()
		web.Header()
		web.Footer()
		return


	name = web.GetData('name')
	if web.GetData('submit'):
		if name:
			if name != web.user.name:
				web.user.name = name
				web.user.active = True
				web.user.Save()
				message = "Name updated"
		else:
			web.user.name = name
			web.user.active = False
			web.user.Save()
			message = "Account disabled. Please set a name."
	else:
		name = web.user.name

	web.Header()
	web.Write("""<br />""")

	if message:
		web.Write("""<div class="notice">%s</div>""" % (message))


	web.Write("""<br /><fieldset><legend>User info</legend>""")
	web.Write("""<form method="post" name="user_info" >""")

	web.Write("""<table class="form_table">""")


	web.Write("""<tr>""")
	web.Write("""<td>Name</td>""")
	web.Write("""<td><input type="text" name="name" value="%s" /></td>""" % (name))
	web.Write("""</tr>""")


	web.Write("""<tr>""")
	web.Write("""<td></td>""")
	web.Write("""<td><input type="submit" name="submit" value="Submit" /></td>""")
	web.Write("""</tr>""")


	web.Write("""</form>""")
	web.Write("""</table>""")
	web.Write("""</fieldset><br /><br />""")

	web.Footer()



